package com.unittec.zk.provider.exam.controller;

import com.alibaba.fastjson.JSON;
import com.unittec.zk.provider.exam.service.ElExamService;
import com.unittec.zk.provider.exam.service.ElPaperService;
import com.unittec.zk.provider.log.Log;
import com.unittec.zk.provider.utils.ValidationUtils;
import com.unittec.zk.sdk.exam.request.*;
import com.unittec.zk.sdk.exam.response.Res201000;
import com.unittec.zk.sdk.log.enums.LogOperateEnum;
import com.unittec.zk.sdk.log.enums.LogStatusEnum;
import com.unittec.zk.sdk.root.domain.GatewayRequest;
import com.unittec.zk.sdk.root.domain.Result;
import com.unittec.zk.sdk.root.enums.BizCode;
import com.unittec.zk.sdk.root.enums.BizCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Api(tags = "在线考试")
public class OnLineController {

    @Resource
    private ElExamService elExamService;

    @Resource
    private ElPaperService elPaperService;
    
    @Resource
    private Log log;
    
    //在线考试-分页
    @RequestMapping(value = "/201017", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "在线考试-分页", notes = "在线考试-分页")
    public Result<Res201000> getOnLinePage(@RequestBody GatewayRequest<Req201017> req) {
        log.debug(req, "getOnLinePage {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res201000 res201000 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            Object o = elExamService.getOnLinePage(req.getContent());
            res201000 = new Res201000();
            res201000.setObject(o);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("201017", JSON.toJSONString(req), "在线考试-分页", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "getOnLinePage {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("201017", JSON.toJSONString(req), "在线考试-分页", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res201000);
        } else {
            log.addOperateLog("201017", JSON.toJSONString(req), "在线考试-分页", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }

    //在线考试-详情
    @RequestMapping(value = "/201018", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "在线考试-详情", notes = "在线考试-详情")
    public Result<Res201000> getOnLineDetail(@RequestBody GatewayRequest<Req201018> req) {
        log.debug(req, "getOnLineDetail {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res201000 res201000 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            Object o = elExamService.getOnLineDetail(req.getContent());
            res201000 = new Res201000();
            res201000.setObject(o);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("201018", JSON.toJSONString(req), "在线考试-详情", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "getOnLineDetail {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("201018", JSON.toJSONString(req), "在线考试-详情", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res201000);
        } else {
            log.addOperateLog("201018", JSON.toJSONString(req), "在线考试-详情", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }
    
    //创建试卷
    @RequestMapping(value = "/201019", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "创建试卷", notes = "创建试卷")
    public Result<Res201000> createPaper(@RequestBody GatewayRequest<Req201019> req) {
        log.debug(req, "createPaper {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res201000 res201000 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            Object o = elPaperService.createPaper(req.getContent());
            res201000 = new Res201000();
            res201000.setObject(o);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("201019", JSON.toJSONString(req), "创建试卷", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "createPaper {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("201019", JSON.toJSONString(req), "创建试卷", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res201000);
        } else {
            log.addOperateLog("201019", JSON.toJSONString(req), "创建试卷", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }
    
    //在线考试-试卷详情
    @RequestMapping(value = "/201020", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "在线考试-试卷详情", notes = "在线考试-试卷详情")
    public Result<Res201000> getPaperDetail(@RequestBody GatewayRequest<Req201020> req) {
        log.debug(req, "getPaperDetail {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res201000 res201000 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            Object o = elPaperService.getPaperDetail(req.getContent());
            res201000 = new Res201000();
            res201000.setObject(o);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("201020", JSON.toJSONString(req), "在线考试-试卷详情", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "getPaperDetail {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("201020", JSON.toJSONString(req), "在线考试-试卷详情", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res201000);
        } else {
            log.addOperateLog("201020", JSON.toJSONString(req), "在线考试-试卷详情", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }

    //在线考试-问题详情
    @RequestMapping(value = "/201021", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "在线考试-问题详情", notes = "在线考试-问题详情")
    public Result<Res201000> getQuDetail(@RequestBody GatewayRequest<Req201021> req) {
        log.debug(req, "getQuDetail {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res201000 res201000 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            Object o = elPaperService.getQuDetail(req.getContent());
            res201000 = new Res201000();
            res201000.setObject(o);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("201021", JSON.toJSONString(req), "在线考试-问题详情", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "getQuDetail {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("201021", JSON.toJSONString(req), "在线考试-问题详情", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res201000);
        } else {
            log.addOperateLog("201021", JSON.toJSONString(req), "在线考试-问题详情", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }

    //在线考试-填答案
    @RequestMapping(value = "/201022", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "在线考试-填答案", notes = "在线考试-填答案")
    public Result<Res201000> fillAnswer(@RequestBody GatewayRequest<Req201022> req) {
        log.debug(req, "fillAnswer {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res201000 res201000 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            Object o = elPaperService.fillAnswer(req.getContent());
            res201000 = new Res201000();
            res201000.setObject(o);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("201022", JSON.toJSONString(req), "在线考试-填答案", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "fillAnswer {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("201022", JSON.toJSONString(req), "在线考试-填答案", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res201000);
        } else {
            log.addOperateLog("201022", JSON.toJSONString(req), "在线考试-填答案", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }
    
    //交卷操作
    @RequestMapping(value = "/201023", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "交卷操作", notes = "交卷操作")
    public Result<Res201000> handleExam(@RequestBody GatewayRequest<Req201023> req) {
        log.debug(req, "handleExam {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res201000 res201000 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            Object o = elPaperService.handleExam(req.getContent());
            res201000 = new Res201000();
            res201000.setObject(o);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("201023", JSON.toJSONString(req), "交卷操作", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "handleExam {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("201023", JSON.toJSONString(req), "交卷操作", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res201000);
        } else {
            log.addOperateLog("201023", JSON.toJSONString(req), "交卷操作", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }
    
    //在线考试-考试结果
    @RequestMapping(value = "/201024", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "在线考试-考试结果", notes = "在线考试-考试结果")
    public Result<Res201000> getPaperResult(@RequestBody GatewayRequest<Req201024> req) {
        log.debug(req, "getPaperResult {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res201000 res201000 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            Object o = elPaperService.getPaperResult(req.getContent());
            res201000 = new Res201000();
            res201000.setObject(o);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("201024", JSON.toJSONString(req), "在线考试-考试结果", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "getPaperResult {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("201024", JSON.toJSONString(req), "在线考试-考试结果", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res201000);
        } else {
            log.addOperateLog("201024", JSON.toJSONString(req), "在线考试-考试结果", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }
}
